package dilmaj.shared;

import java.util.*;

import javax.jdo.annotations.Persistent;

import com.google.gwt.user.client.rpc.IsSerializable;

import dilmaj.client.StarButtonPanel;
import dilmaj.client.StarEntryJDO;
import dilmaj.client.TermJDO;

public class StarEntryVO implements IsSerializable {
    protected Long id;
    
    protected Long rank;
    
	protected String caption;
      
	protected String title;
	
	protected String description;
	
	protected Long userID;
	protected String userName;
    
    protected List<StarEntryVO> attachments=new ArrayList<StarEntryVO>();
    
    private String attachmentString="";
    
    /*private StarButtonPanel buttonPanel=null;
    
    public void setButtonPanel(StarButtonPanel buttonPanel) {
    	this.buttonPanel=buttonPanel;
    }
    
    public StarButtonPanel getButtonPanel() {
    	return buttonPanel;
    }*/
    
    public StarEntryVO() {
    }
    
    public StarEntryVO(StarEntryJDO starJDO) {
    	this.caption=starJDO.getCaption();
    	this.title=starJDO.getTitle();
    	this.id=starJDO.getId();
    	this.description=starJDO.getDescription();
    	this.rank=starJDO.getRank();
    	
    	userID=starJDO.getUserID();
    	userName=starJDO.getUsername();
    	
    	attachmentString=starJDO.getAttachmentsAsString();
	}

    public void setDescription(String description) {
    	this.description=description;
    }
    
    public String getDescription() {
    	return description;
    }
    
    public Long getId() {
    	return id;
    }
    
    public void setId(Long id) {
    	this.id=id;
    }
    
    public void addAttachment(StarEntryVO newAttachment) {
    	attachments.add(newAttachment);
    }
    
    public void removeAttachment(StarEntryVO newAttachment) {
    	attachments.remove(newAttachment);
    }
    
    public boolean has(StarEntryVO attachment) {
    	return attachments.contains(attachment);
    }
    
    public void setCaption(String caption) {
    	this.caption=caption;
    }
    
    public String getCaption() {
    	return caption;
    }
    
    public String getTitle() {
    	return title;
    }

	public List<StarEntryVO> getAttachments() {
		// TODO Auto-generated method stub
		return attachments;
	}
	
	public List<Long> getAttachmentIDs() {
		List<Long> ids=new ArrayList<Long>();
		
		Iterator iterator=attachments.iterator();
		while (iterator.hasNext()) {
			StarEntryVO currentVO=(StarEntryVO)iterator.next();
			ids.add(currentVO.getId());
		}
		
		return ids;
	}
	
	public void setRank(long rank) {
		this.rank=rank;
	}
	
	public long getRank() {
		if (this.rank==null)
			return 0; 
		return rank;
	}
	
	public List<StarEntryVO> getAttachmentsOfType(String type) {
		List<StarEntryVO> foundAttachments=new ArrayList<StarEntryVO>();
		
		Iterator iterator=attachments.iterator();
		while (iterator.hasNext()) {
			StarEntryVO currentAttachment=(StarEntryVO)iterator.next();
			
			if (currentAttachment.getTitle().compareToIgnoreCase(type)==0)
				foundAttachments.add(currentAttachment);
		}
		
		return foundAttachments;
	}
	
	public void setUserName(String username) {
		userName=username;
	}
	
	public void setUserID(Long userid) {
		userID=userid;
	}
	
	public String getUsername() {
		return userName;
	}
	
	public Long getUserID() {
		return userID;
	}
	
	public List<StarPrivilegeVO> getPrivileges() {
		List<StarPrivilegeVO> privileges=new ArrayList<StarPrivilegeVO>();
		
		Iterator iterator=attachments.iterator();
		while (iterator.hasNext()) {
			try {
				StarPrivilegeVO currentPrivilege=(StarPrivilegeVO)iterator.next();
				privileges.add(currentPrivilege);
			} catch (ClassCastException cce) {
				
			}
		}
		
		return privileges;
	}
	
	public boolean isPrivilegedBy(String username) {
		Iterator iterator=attachments.iterator();
		while (iterator.hasNext()) {
			try {
				StarPrivilegeVO currentPrivilege=(StarPrivilegeVO)iterator.next();
				if (currentPrivilege.getUsername().compareTo(username)==0)
					return true;
			} catch (ClassCastException cce) {
				
			}
		}
		
		return false;
	}
	
	public String getAttachmentString() {
		return attachmentString;
	}
}